package com.woniuxy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniuxy.annotation.MultiRequestBody;
import com.woniuxy.exception.customException.AddFailedException;
import com.woniuxy.exception.customException.TheAddedHasBeenExist;
import com.woniuxy.resultCode.Result;
import com.woniuxy.system.entity.TempUser;
import com.woniuxy.system.service.PerformanceUserService;
import com.woniuxy.system.service.TempUserService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 考核模板、用户中间表 前端控制器
 * </p>
 *
 * @author woniuxy
 * @since 2022-06-13
 */
@RestController
@RequestMapping("/system/tempUser")
@RequiresAuthentication
public class TempUserController {
    @Autowired
    TempUserService tempUserService;
    @Autowired
    PerformanceUserService userService;

    @PostMapping("/AddTemplate")
    @RequiresRoles(value={"1","2"},logical = Logical.OR)
    public Result AddTemplate(@MultiRequestBody(required = false) String userid,@MultiRequestBody(required = false) String TempName) throws AddFailedException, TheAddedHasBeenExist {
                LambdaQueryWrapper<TempUser> tempUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tempUserLambdaQueryWrapper.eq(TempUser::getName,TempName);
        List<TempUser> list = tempUserService.list(tempUserLambdaQueryWrapper);
        if(list.size()>0){
            throw new TheAddedHasBeenExist();
        }
        TempUser tempUser = new TempUser();
        tempUser.setUserId(Long.valueOf(userid));
        tempUser.setName(TempName);
        boolean save = tempUserService.save(tempUser);
        if(!save){
            throw new AddFailedException();
        }
        return Result.ok().message("添加成功");
    }

    @GetMapping("listTemplate")
    Result listTemplate(){
//        LambdaQueryWrapper<TempUser> tempUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        tempUserLambdaQueryWrapper.eq(TempUser::getUserId,userid);
        List<TempUser> list = tempUserService.getAllTemplate();
        return Result.ok().data("msg",list);
    }
    @DeleteMapping("/deleteTemplateById/{id}")
    @RequiresRoles(value={"1","2"},logical = Logical.OR)
    Result deleteTemplateById(@PathVariable String id){
        boolean b = tempUserService.removeById(id);
        if(b){
            return Result.ok().message("删除成功");
        }
        return Result.error().message("删除失败");
    }
    @GetMapping("/getTemplateInfoByid/{id}")
    @RequiresRoles(value={"1","2"},logical = Logical.OR)
    Result getTemplateInfoByid(@PathVariable String id){
        TempUser info = tempUserService.getById(id);
        return Result.ok().data("msg",info);
    }
}

